<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/9/5 0005
 * Time: 上午 9:35
 */
namespace app\admin\controller;

use app\common\model\User;
use think\Controller;
use app\common\model\StationContent;
use app\common\model\UserStationLetter;
use think\Session;

/*
 * 站内信
 */

class Letter extends Base{
    public $stationContentDao;
    public $userStationLetter;
    public $userDao;
    public $platformType;
    public function _initialize()
    {
        parent::_initialize();
       $this->stationContentDao = new StationContent();
       $this->userStationLetter = new UserStationLetter();
       $this->userDao           = new User();
       $this->platformType      = 0;
    }

    //首页
    public function index(){
        $keys = input('key');
        $type = input('type')!=null?input('type'):1;
        $map = [];
        if($keys&&$keys!=="")
        {
            $map['b.content'] = ['like',"%" . $keys . "%"];
        }
        if($type=='1'){
            $map['received_user_id'] =session($this->set['db_name'].'uid');
            $map['received_user_status'] = $this->platformType ;
            $map['status'] = ['not in','(0,2)'];
        }else{
            $map['send_user_id'] =session($this->set['db_name'].'uid');
            $map['send_user_tatus'] = $this->platformType ;
            $map['status'] = ['not in','(0,3)'];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('pagesize');// 获取总条数
        $count = $this->userStationLetter->alias('a')->join('station_content'.' b','a.m_id = b.message_id','left')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $lists = $this->userStationLetter->where($map)
            ->alias('a')
            ->field('*')
            ->join('station_content'.' b','a.m_id = b.message_id','left')
            ->page($Nowpage, $limits)->order('is_status desc,id desc')->select();
        if($type==1){
            $typetitle = 'send_user_id';
            $typestatus = 'send_user_tatus';
            //收发列表标识
            $redtype    = 1;
        }else{
            $typetitle = 'received_user_id';
            $typestatus = 'received_user_status';
            $redtype    = 2;
        }
        foreach ($lists as $key=>$val){
            if($val->$typestatus==0){
                $ret = $this->userDao->getOne('account,name',array('id'=>$val->$typetitle),'admin_user');
                $val->message_id = lang('HEADQUARTERS').'-'.$ret['name'];
                $val->redtype = $redtype;
            }
            if($val->$typestatus==2){
                $ret = $this->userDao->getOperationUserInfoOne('a.*,b.title,c.company_name',array('a.id'=>$val->$typetitle),'operation_user','operation_auth_group','c.company_name desc');
                $val->message_id = $ret['company_name'].'-'.$ret['title'].'-'.$ret['name'];
                $val->redtype = $redtype;
            }
            if($val->$typestatus==1){
                $ret = $this->userDao->getAdminUserInfoOne('a.*,b.title,c.company_name',array('a.id'=>$val->$typetitle),'administrative_user','administrative_auth_group','c.company_name desc');
                $val->message_id = $ret['company_name'].'-'.$ret['title'].'-'.$ret['name'];
                $val->redtype = $redtype;
            }

        }
        //收发件箱读取数据
        $rmap['received_user_id'] =session($this->set['db_name'].'uid');
        $rmap['received_user_status'] = $this->platformType ;
        $rmap['status'] = ['not in','(0,2)'];
        $zrcount = $this->userStationLetter->where($rmap)->count();
        $rmap['is_status'] = 2;
        $rcount = $this->userStationLetter->where($rmap)->count();
        $smap['send_user_id'] =session($this->set['db_name'].'uid');
        $smap['send_user_tatus'] = $this->platformType ;
        $smap['status'] = ['not in','(0,3)'];
        $zscount = $this->userStationLetter->where($smap)->count();
        $smap['is_status'] = 2;
        $scount = $this->userStationLetter->where($smap)->count();
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        if(input('get.page')){
            $data =array();
            $data['count'] = $count;
            $data['data'] = $lists;
            $data['allpage'] = $allpage;
            $data['zrcount'] = $zrcount;
            $data['rcount'] = $rcount;
            $data['zscount'] = $zscount;
            $data['scount'] = $scount;
            return json($data);
        }
        $this->assign('val', $keys);
        $this->view->rcount = $rcount;
        $this->view->userslist=$this->stationContentDao->getUsers();
        $this->view->alert= parseAlert();
        return $this->fetch();
    }
    //查看收件站内信
    public function lookmail(){
        $id = input('id');
        $letterInfo = $this->userStationLetter->getLetterInfo($id);
        $this->userStationLetter->mod_is_status($id);
        $this->view->data = $letterInfo;
        $this->view->redtype    = 1;
        return $this->fetch();
    }
    //查看发件站内信
    public function rlookmail(){
        $id = input('id');
        $letterInfo = $this->userStationLetter->getLetterInfo($id);
        $this->view->data = $letterInfo;
        $this->view->redtype    = 2;
        return $this->fetch('lookmail');
    }


    //发送信息
    public function send(){
        $post = input('');
        $content = $post['content'];
        $adminsSubset = isset($post['adminsSubset'])?$post['adminsSubset']:array();
        $operationSubset = isset($post['operationSubset'])?$post['operationSubset']:array();
        $adminSubset = isset($post['adminSubset'])?$post['adminSubset']:array();
        if(trim($content)==""){
            alert('error',lang('CONTENT CAN NOT NULL'),url('Letter/index'));
        }
        if(!$adminsSubset && !$operationSubset && !$adminSubset){
            alert('error',lang('ADDRESSEE NOT EMPTY'),url('Letter/index'));
        }
        $data = array();
        $data['content'] = $content;
        $data['adminsSubset'] = $adminsSubset;
        $data['operationSubset'] = $operationSubset;
        $data['adminSubset'] = $adminSubset;
        $send_user_id = session($this->set['db_name'].'uid');
        $ret = $this->userStationLetter->sendMessageContent($data,$send_user_id,0);
        if($ret){
            alert('success',lang('CONTENT SEND SUCCESS'),url('Letter/index'));
        }else{
            alert('error',lang('CONTENT SEND FAILURE'),url('Letter/index'));
        }
    }

    /*
     * 删除站内信
     */
    public function del(){
        $id = input('id');
        $redtype = input('redtype');
        $ret = $this->userStationLetter->del($id,$redtype);
        if($ret){
            return json(['code'=>1,'msg'=>lang('DEL LETTER SUCCESS')]);
        }else{
            return json(['code'=>0,'msg'=>lang('DEL LETTER FAILURE')]);
        }
    }


    /*
     * 删除多站内信
     */
    public function dels(){
        $post = input('');
        $ids = $post['ids'];
        $redtype = $post['redtype'];
        $onkey = array_search('on',$ids);
        if($onkey!==false){
            unset($ids[$onkey]);
        }
        $ret = $this->userStationLetter->dels($ids,$redtype);
        if($ret){
            return json(['code'=>1,'msg'=>lang('DEL LETTER SUCCESS')]);
        }else{
            return json(['code'=>0,'msg'=>lang('DEL LETTER FAILURE')]);
        }
    }

    /*
     * 阅读多站内信
     */
    public function reds(){
        $post = input('');
        $ids = $post['ids'];
        $redtype = $post['redtype'];
        if($redtype==0){
            return json(['code'=>0,'msg'=>lang('PROHIBIT REDS LETTER')]);
        }
        $onkey = array_search('on',$ids);
        if($onkey!==false){
            unset($ids[$onkey]);
        }
        $ret = $this->userStationLetter->mod_is_statuss($ids,$redtype);
        if($ret){
            return json(['code'=>1,'msg'=>lang('RED LETTER SUCCESS')]);
        }else{
            return json(['code'=>0,'msg'=>lang('RED LETTER FAILURE')]);
        }
    }

    /*
     * ajax轮询查询数据
     */
    public function tips(){
        $rmap['received_user_id'] =session($this->set['db_name'].'uid');
        $rmap['received_user_status'] = $this->platformType ;
        $rmap['status'] = ['not in','(0,2)'];
        $rmap['is_status'] = 2;
        $rcount = $this->userStationLetter->where($rmap)->count();
        return json(['rcount'=>$rcount]);
    }
}